﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Xml;

namespace MultiTouch.Behaviors.Silverlight4.TouchPPT
{
    /// <summary>
    /// Simple ViewModel for the MainPage
    /// </summary>
    public class MainPageViewModel : INotifyPropertyChanged
    {
        private string _catalogueFile = "catalogue.xml";
        private string _imageFile = "images.xml";
        private string _mediaFile = "media.xml";

        public MainPageViewModel()
        {
            // Read the list of all
            // XmlXapResolver is the default resolver.
            using (XmlReader reader = XmlReader.Create(_catalogueFile))
            {
                // Moves the reader to the root element.
                reader.MoveToContent();
                while (reader.ReadToFollowing("string"))
                {
                    string s = reader.ReadInnerXml();
                    if (s.Length > 0)
                    {
                        _catalogue.Add(s);
                    }
                }
            }

            // Read the media file list
            // XmlXapResolver is the default resolver.
            using (XmlReader reader = XmlReader.Create(_mediaFile))
            {
                // Moves the reader to the root element.
                reader.MoveToContent();
                while (reader.ReadToFollowing("string"))
                {
                    string s = reader.ReadInnerXml();
                    if (s.Length > 0)
                    {
                        _mediaList.Add(s);
                    }
                }
            }

            // XmlXapResolver is the default resolver.
            using (XmlReader reader = XmlReader.Create(_imageFile))
            {
                // Moves the reader to the root element.
                reader.MoveToContent();
                while (reader.ReadToFollowing("string"))
                {
                    string s = reader.ReadInnerXml();
                    if (s.Length > 0)
                    {
                        List<string> list = StringParser.ParseStringIntoList(s);
                        if (list != null && list.Count > 0)
                        {
                            ObservableCollection<BitmapImage> c = new ObservableCollection<BitmapImage>();
                            foreach (string str in list)
                            {
                                c.Add(new BitmapImage(new Uri(str, UriKind.RelativeOrAbsolute)));
                            }
                            _picturesCollection.Add(c);
                        }
                    }
                }
            }

            for (int i = 0; i < _picturesCollection.Count; i++)
            {
                _selectedPictures.Add(_picturesCollection[i].FirstOrDefault());
            }
        }

        public int Count
        {
            get { return _picturesCollection.Count; }
        }

        private List<string> _catalogue = new List<string>();
        public List<string> Catalogue
        {
            get { return _catalogue; }
        }

        private List<string> _mediaList = new List<string>();
        public List<string> MediaList
        {
            get { return _mediaList; }
        }

        /// <summary>
        /// Selected picture index
        /// </summary>
        // The first set of pictures is selected by default
        private int _selectedPictureIndex = 0;
        public int SelectedPictureIndex
        {
            get { return _selectedPictureIndex; }
            set
            {
                if (_selectedPictureIndex != value && value >= 0 && value < Count)
                {
                    _selectedPictureIndex = value;
                    RaisePropertyChanged("SelectedPictureIndex");
                    RaisePropertyChanged("SelectedPictureCollection");
                }
            }
        }

        private List<ObservableCollection<BitmapImage>> _picturesCollection = new List<ObservableCollection<BitmapImage>>();
        public ObservableCollection<BitmapImage> SelectedPictureCollection
        {
            get { return _picturesCollection[_selectedPictureIndex]; }
        }

        /// <summary>
        /// The Selected picture
        /// </summary>
        private List<BitmapImage> _selectedPictures = new List<BitmapImage>();
        public List<BitmapImage> SelectedPictures
        {
            get { return _selectedPictures; }
        }

        public BitmapImage SelectedPictureByIndex
        {
            get
            {
                if (_selectedPictureIndex >= 0 && _selectedPictureIndex < _selectedPictures.Count)
                {
                    return _selectedPictures[SelectedPictureIndex];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (value != null)
                {
                    _selectedPictures[_selectedPictureIndex] = value;
                }
                else
                {
                    _selectedPictures[_selectedPictureIndex] = _picturesCollection[_selectedPictureIndex].FirstOrDefault();
                }
                RaisePropertyChanged("SelectedPictureByIndex");
                RaisePropertyChanged("SelectedPictures");
            }
        }

        #region INotifyPropertyChanged implementation
        public event PropertyChangedEventHandler PropertyChanged;

        void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        #endregion
    }
}
